package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.ying.space.core.dao.SearchPickGoodsDAO;
import com.ecoolex.ying.space.entity.SearchPickGoods;
import com.ecoolex.ying.space.entity.SearchPickGoods.Column;
import com.ecoolex.ying.space.entity.SearchPickGoodsExample;
import com.ecoolex.ying.space.entity.SearchPickGoodsWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Consumer;

import static com.ecoolex.ying.space.entity.SearchPickGoods.NOT_DELETED;
import static com.ecoolex.ying.space.entity.SearchPickGoodsExample.*;

@Service
public class SearchPickGoodsManager {
    @Autowired
    private SearchPickGoodsDAO dao;

    public SearchPickGoodsWithBLOBs getByPickGoodsId(Integer pickGoodsId) {
        SearchPickGoodsExample example = new SearchPickGoodsExample();
        example.createCriteria()
                .andPickGoodsIdEqualTo(pickGoodsId)
                .andDeletedEqualTo(NOT_DELETED);

        return dao.selectOneByExampleWithBLOBs(example);
    }

    private Pagination<SearchPickGoodsWithBLOBs> query(Consumer<Criteria> consumer, Page page) {
        SearchPickGoodsExample example = new SearchPickGoodsExample();
        Criteria criteria = example.createCriteria();

        consumer.accept(criteria);

        criteria.andDeletedEqualTo(NOT_DELETED);
        // 排序
        example.orderBy(Column.pickGoodsId.asc());
        // 分页
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExampleWithBLOBs(example));
    }

    public Pagination<SearchPickGoodsWithBLOBs> queryByGoodsId(Integer goodsId, Page page) {
        return query(criteria -> criteria.andGoodsIdEqualTo(goodsId), page);
    }

    public Pagination<SearchPickGoodsWithBLOBs> queryByBrandId(Integer brandId, Page page) {
        return query(criteria -> criteria.andBrandIdEqualTo(brandId), page);
    }

    public Pagination<SearchPickGoodsWithBLOBs> queryByClassifyId(Integer classifyId, Page page) {
        return query(criteria -> criteria.andClassifyIdEqualTo(classifyId), page);
    }

    public Pagination<SearchPickGoodsWithBLOBs> queryByParentClassifyId(Integer parentClassifyId, Page page) {
        return query(criteria -> criteria.andParentClassifyIdEqualTo(parentClassifyId), page);
    }
}